<template>
	<view class="news-content">
		<view class="title">{{data.title}}</view>
		<view class="auth">
			<image class="avatar" :src="data.author_avatar"></image>
			<view class="auth-name">{{data.author}}</view>
		</view>
		<view class="content">
			<mp-html :content="data.content"  ></mp-html>
		</view>
		
		<view class="edit">编辑丨{{data.editor}}  主编丨{{data.chief_editor}}  图源丨{{data.img_source}}</view>
	</view>
	<view class="news-comment" v-if="data.comment">
		<view class="title">精选评论</view>
		<view class="comment-item" v-for="item in data.comment" :key="item.id">
			<view class="comment-auth">
				<image class="avatar" :src="item.avatar"></image>
				<view class="info">
					<view class="name">{{item.name}}</view>
					<view class="time">{{item.created_at}} {{item.city}}</view>
				</view>
			</view>
			<view class="comment-content">
				{{item.content}}
			</view>
		</view>
	</view>
	<view style="width: 100%;height: 150rpx;"></view>
	<view class="footer-show">
		<view class='send-box'>
			<input class="uni-input send-box-input"  placeholder="&nbsp;&nbsp;发布评论" v-model="content" @confirm="comment"/>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				newsId:0,
				data:{
					comment:[],
				},
				content:'',
				htmlContent:'',
				lng:0,
				lat:0,
				images: [], // 存储图片链接
				nodes: []
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
			  console.log(res.target)
			}
			return {
			  title: this.data.title,
			  path: '/pages/news/detail?newsId='+this.newsId
			}
		},
		mounted() {
			   let routes = getCurrentPages();
			   let options = routes[routes.length - 1].options; 
			   if (options.newsId) {
			   	 this.newsId = options.newsId;
			   	 this.getNewsDetail();
			   }
		  },
		onLoad(options) {
		},
		methods: {
			// 获取资讯详情
			async getNewsDetail(refresh = false) {
				const user = uni.getStorageSync('history-user');
				var user_id = 0;
				if(user) {
					user_id = user.id;
				}
				const res = await this.$request.get('/api/news/detail',{
					news_id: this.newsId,
					user_id: user_id,
				})
				if (res.code == 200) {
					this.data = res.data
				}else{
					uni.showToast({
						title: '获取信息失败',
						icon: 'none'
					})
					return
				}
			},
			async comment() {
				//未登录 直接返回
				if (uni.getStorageSync('userInfo') =="") {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500);
					return;
				}
				var that = this;
				uni.getLocation({
					type: 'wgs84',
					geocode:true,//设置该参数为true可直接获取经纬度及城市信息
					success: function (res) {
						that.doComment(res.longitude,res.latitude);
					},
					fail: function () {
						uni.showToast({
							title: '获取地址失败，将导致部分功能不可用',
							icon:'none'
						});
					}
				});

			},
			doComment(lng, lat) {
				const ret = this.$request.post('/api/news/comment',{
					news_id: Number(this.newsId),
					content: this.content,
					lng: lng,
					lat: lat
				})
				this.content = '';
				uni.showToast({
					title:"评论成功",
					icon: 'none'
				});
			},
		},

	}
</script>

<style scoped lang="scss">
.news-content{
	width: 718rpx;
	height: auto;
	padding-bottom: 32rpx;
	background: #FFFFFF;
	border-radius: 24rpx 24rpx 24rpx 24rpx;
	margin:0 auto;
    margin-top: 8rpx;
	overflow: hidden;
	.title{
		width: 686rpx;
		height: auto;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 40rpx;
		color: #262626;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin:0 auto;
		margin-top:32rpx;
	}
	.auth{
		width: 686rpx;
		height: 56rpx;
		margin: 0 auto;
		margin-top:32rpx;
		margin-bottom:32rpx;
		.avatar{
			width: 56rpx;
			height: 56rpx;
			border-radius: 56rpx;
			float: left;
		}
		.auth-name{
			float: left;
			margin-left:8rpx;
			width: auto;
			padding-top:10rpx;
			height: 36rpx;
			line-height: 36rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #7B7B7B;
			text-align: left;
			font-style: normal;
			text-transform: none;
		}
	}
	.content{
		width: 686rpx;
		height: auto;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #262626;
		line-height: 45rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin:0 auto;
	}
	.edit{
		width: 686rpx;
		height: auto;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #7B7B7B;
		line-height: 35rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin-left: 16rpx;
		padding-top:32rpx;
	}
}
.news-comment{
	width: 718rpx;
	height: auto;
	padding-bottom: 32rpx;
	background: #FFFFFF;
	border-radius: 24rpx 24rpx 24rpx 24rpx;
	margin:0 auto;
    margin-top:16rpx;
	overflow: hidden;
	.title{
		clear: both;
		display: block;
		width: 200rpx;
		height: 38rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #000000;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin-left:16rpx;
		margin-top:32rpx;
		margin-bottom:24rpx;
	}
	.comment-item{
		.comment-auth {
			width: 686rpx;
			height: 72rpx;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			margin: 0 auto;
			margin-left: 16rpx;
			.avatar{
				width: 72rpx;
				height: 72rpx;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				float: left;
			}
			.info{
				float: left;
				width: 500rpx;
				height: 72rpx;
				margin-left: 20rpx;
				.name{
					width: 100%;
					height: 36rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #262626;
					text-align: left;
					font-style: normal;
					text-transform: none;
				}
				.time{
					width: 100%;
					height: 30rpx;
					line-height: 30rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #B1B1B1;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin-top:10rpx;
				}
				
			}
		}
		.comment-content{
			width: 588rpx;
			height: 72rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #262626;
			text-align: left;
			font-style: normal;
			text-transform: none;
			margin: 0 auto;
			margin-left: 100rpx;
		    margin-top:16rpx;
		}
	}
}
.footer-show{
	position: fixed;
	width: 100%;
	height: 140rpx;
	background: #FFFFFF;
	border-radius: 0rpx 0rpx 0rpx 0rpx;
	bottom: 0rpx;
	z-index:9999;
	.send-box{
		width: 686rpx;
		height: 72rpx;
		background: #F5F5F5;
		border-radius: 36rpx 36rpx 36rpx 36rpx;
		margin: 0 auto;
		margin-top:20rpx;
		.send-box-input{
			width: 600rpx;
			height: 72rpx;
			line-height: 72rpx;
			background: #F5F5F5;
			border-radius: 36rpx 36rpx 36rpx 36rpx;
			margin: 0 auto;
		}
	}
}
</style>
